package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.darkforest.auth.CurrentMember;
import org.darkforest.dao.mapper.EnumParamMapper;
import org.darkforest.dao.mapper.InvoiceMapper;
import org.darkforest.model.EnumParam;
import org.darkforest.model.Invoice;
import org.darkforest.model.Member;
import org.darkforest.model.RestEntity;
import org.darkforest.auth.AuthPassport;
import org.darkforest.vo.MemberBase;
import org.darkforest.vo.WapAjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Libing on 2015/3/22.
 */
@RestController
@RequestMapping("invoice")
@Api(value = "invoice", description = "发票",produces= MediaType.APPLICATION_JSON_VALUE)
public class InvoiceController {

    @Autowired
    private InvoiceMapper invoiceMapper;

    @Autowired
    private EnumParamMapper enumParamMapper;
    @AuthPassport
    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ApiOperation(value = "获取发票列表", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity getInvoicelist(@CurrentMember Member member){
        try{
            return RestEntity.success(invoiceMapper.selectInvoiceByMember(member.getMemberId()));
        }catch (Exception e){
            return RestEntity.failed();
        }
    }

    /**
     * ajax添加，报错成功时返回包含主键的对象
     * @param invoice
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ApiOperation(value = "新增发票", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity save(@CurrentMember Member member, @RequestBody Invoice invoice){

            invoice.setInvState("1");
            invoice.setMemberId(member.getMemberId());
            invoice.setInvTitle(StringUtils.isBlank(invoice.getInvTitle())?"个人":invoice.getInvTitle());
            int save = invoiceMapper.insert(invoice);
            if(save>0){
                return RestEntity.success(invoice);
            }else{
                return RestEntity.failed("发票信息报错失败");
            }
    }

    /**
     * ajax删除
     * @param invId
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "delete",method = RequestMethod.GET)
    @ApiOperation(value = "删除发票", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity deteleInvoice( @RequestParam(required = true) Integer invId){
        Integer res = invoiceMapper.deleteByPrimaryKey(invId);
        if(res>0){
            return RestEntity.success("删除成功");
        }else{
            return RestEntity.failed();
        }
    }


    @AuthPassport
    @RequestMapping(value = "contentList",method = RequestMethod.GET)
    @ApiOperation(value = "获取发票类型", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity getInvoiceContentlist(){
        try{
            List<EnumParam> enumParams = enumParamMapper.selectListByCondition(new EnumParam("invoice_content"));
            List<String> list = new ArrayList<String>();
            for(EnumParam enumParam:enumParams){
                list.add(enumParam.getParamValue());
            }
            return RestEntity.success(list);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }
}
